$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'aic/ctoctransaction/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true },
			{ label: '操作', name: 'transProofImg', index: 'trans_proof_img', width: 65,
				formatter: function(value, options, row) {
					var retVal = "<a class='label label-primary' href='javascript:vm.getInfo(" + row.id + ")'>交易详情</a>&nbsp;"
					if (value) {
						retVal += "<a target='_blank' style='display: inline-block;' class='label label-primary' href='" + value + "'>交易凭证</a>"
					}
					return retVal;
				}
			},
			{ label: '订单号', name: 'orderNo', index: 'order_no', width: 100 },
			{ label: '买卖用户', name: 'buyerRealname', index: 'buyer_realname', width: 140,
				formatter: function(value, options, row) {
					return "买家:" + row.buyerRealname + ":" + row.buyerMobile + "<br/>卖家:" + row.sellerRealname + ":" + row.sellerMobile;
				}
			},
			{ label: '单价', name: 'coinPrice', index: 'coin_price', width: 50 }, 			
			{ label: '数量', name: 'coinQuantity', index: 'coin_quantity', width: 50 }, 			
			{ label: '总金额', name: 'coinTotalPrice', index: 'coin_total_price', width: 60 },
			{ label: '手续费', name: 'transFee', index: 'trans_fee', width: 50 }, 			
			{ label: '交易状态', name: 'status', index: 'status', width: 80,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">等待买家付款</span>';
					} else if (value == 1) {
						return '<span class="label label-info">等待卖家确认</span>';
					} else if (value == 2) {
						return '<span class="label label-success">订单完成</span>';
					} else if (value == 3) {
						return '<span class="label label-danger">取消</span>';
					}
					return value;
				}
			},
			{ label: '申诉状态', name: 'appealStatus', index: 'appeal_status', width: 80,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">未申诉</span>';
					} else if (value == 1) {
						return '<span class="label label-info">申诉中</span>';
					} else if (value == 2) {
						return '<span class="label label-success">处理完成</span>';
					}
					return value;
				}
			},
			{ label: '下单时间', name: 'createTime', index: 'create_time', width: 110 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{ mobile: null, orderNo: null},
		showList: true,
		ctocTransaction: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		update: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			layer.prompt({title: '请填写处理结果', formType: 2, value : "处理完成"}, function(text, index){
				var url = "aic/ctoctransaction/update";
				$.ajax({
					type: "POST",
				    url: baseURL + url,
				    dataType: "json",
				    data: $.param({ids:ids,message:text},true),
				    success: function(r){
				    	if(r.code === 0){
				    		layer.close(index);
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		sellerConfirm: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('您确定操作？如果订单状态非已付款将跳过处理!', function(){
				var url = "aic/ctoctransaction/sellerConfirm";
				$.ajax({
					type: "POST",
				    url: baseURL + url,
				    dataType: "json",
				    data: $.param({ids:ids},true),
				    success: function(r){
				    	if(r.code === 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			vm.showList = false;
			$.get(baseURL + "aic/ctoctransaction/info/"+id, function(r){
                vm.ctocTransaction = r.ctocTransaction;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		},
		closeDiv: function() {
			vm.showList = true;
		}
	}
});